<template>
  <div class="box">
       <common-head title="京东商城登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          // { pattern: /\w{4,6}/, message: '请输入4到6位字符' }
          /*
          {
              validator: (value) => {
                // 只能输入小明
                if (value === '小明') {
                  return true
                } else {
                  return false
                }
              },
              message: '不要乱输入'}
              */
        ]"
      />
      <van-field
        v-model="userInfo.pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
   <div class="denglu">
    <van-button >手机号登录</van-button>
    <van-button @click="$router.push('/register')">注册账号</van-button>
    </div>
    <div class="qita">
    <h4 class="fangshi">其他登录方式</h4>
   <a href="" class="qq">微信</a>
   <p>未注册的手机号验证后将自动创建京东账号, 登录即代表您已同意<a href="">京东隐私政策</a></p>
    </div>
  </div>
</template>

<script>
import CommonHead from "_components/CommonHead"; 
import { doLogin } from "_api";
import { Toast } from "vant";
export default {
  data() {
    return {
      userInfo: {
        username: "",
        pwd: "",
      },
    };
  },
  methods: {
    onSubmit(params) {
      doLogin({
        //参数多了用户名和密码
        ...params,
        deviceId: 11, //登录设备id
        deviceName: "xxx",
      }).then((res) => {
        if (res.data.code === 0) {
          // 将登录成功的token 缓存起来
          localStorage.setItem("token", res.data.data.token); //时效性是后端控制的
          Toast({
            type: "success",
            message: "登录成功",
            onClose: () => {
              if (this.$route.query.from) {
                // 被拦截 进入登录页 回去
                this.$router.replace(this.$route.query.from);
              } else {
                this.$router.replace("/home");
              }
            },
          });
        } else {
          Toast.fail("登录失败");
        }
      });
    },
  },
   components:{
    CommonHead
  }
};
</script>

<style lang="scss" scoped>
.denglu{
  display: flex;
  justify-content: space-between;

}
.fangshi{
 color: rgba(0,0,0,.2);
 text-align: center;
 margin: -0.5243rem auto 0;
 width: 10.4rem;
 font-size: 14px;
 background: #F3F5F7;

}
.qita{
 border-top: 1px solid rgba(0,0,0,.1);
font-size: 1.12rem;
margin-top: 100px;
width: 80%;
margin-left: 10%;
height: 100px;

}
.qq{
  background: url('~_assets/weixin1.jpg') no-repeat  center;
 background-size: 50% ;
margin-left: 130px;
  display: block;
  padding-top: 60px;
  color: rgba(0,0,0,.2);
  padding-left: 20px;
  width: 4.7027rem;height: 4.7027rem;
}
.qita>p{
  margin-top: 80px;
 
  text-align: center;
   color: rgba(0,0,0,.2);
font-size: 0.74rem;
}
</style>
